package ssn.servicearea.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import ssn.common.code.service.CodeService;
import ssn.common.util.MD5Hash;
import ssn.servicearea.service.ServiceAreaService;
import ssn.servicearea.vo.ServiceAreaManagerVO;
import ssn.servicearea.vo.ServiceAreaVO;
import ssn.user.service.UserService;
import egovframework.rte.fdl.cmmn.AbstractServiceImpl;
import egovframework.rte.fdl.property.EgovPropertyService;
import egovframework.rte.fdl.string.EgovStringUtil;

/**  
 * @Class Name : 
 * @Description : 
 * 
 * @author ksh
 * @since 2013. 09.26
 * @version 1.0
 * @see
 */
@SuppressWarnings({"rawtypes","unchecked"})
@Service("serviceareaService")
public class ServiceAreaServiceImpl extends AbstractServiceImpl implements ServiceAreaService {
	
	@Resource(name="serviceareaDAO")
	private ServiceAreaDAO serviceareaDAO; 
	
	@Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
	
	@Resource(name = "codeService")
    private CodeService codeService;
	
	@Resource(name = "userService")
    private UserService userService;
	
	/**
	 * 휴게소 정보 입력
	 */
	public void insertServiceAreaInfo(ServiceAreaVO vo) throws Exception{
		
		//MD5Hash encoder = new MD5Hash();
		//vo.setSsa_pass_word(encoder.getHash(vo.getSsa_pass_word()));
		
		serviceareaDAO.insertServiceAreaInfo(vo);
		serviceareaDAO.insertServiceAreaMeta(vo);
		this.insertServiceAreaManagerInfo(vo);
	}
	
	/**
	 * 휴게소 관리자 정보 저장
	 * @param vo
	 * @throws Exception
	 */
	public void insertServiceAreaManagerInfo(ServiceAreaVO vo) throws Exception{
		int managerPk = Integer.parseInt(serviceareaDAO.selectServiceAreaManagerInfoPk(vo.getSsa_area_key()));
		for(int i=0;i<vo.getServiceAreaManagerVOList().size();i++){
			vo.getServiceAreaManagerVOList().get(i).setSsm_user_id(String.valueOf(managerPk+i));
		}
		serviceareaDAO.insertServiceAreaManagerInfo(vo);
	}
	
	/**
	 * 휴게소 정보 수정
	 */
	public void updateServiceAreaInfo(ServiceAreaVO vo) throws Exception{
		
		//if(!EgovStringUtil.equals(vo.getSsa_pass_word(), serviceareaDAO.selectServiceAreaPw(vo.getSsa_area_key()))){
			//MD5Hash encoder = new MD5Hash();
			//vo.setSsa_pass_word(encoder.getHash(vo.getSsa_pass_word()));
		//}
		serviceareaDAO.updateServiceAreaInfo(vo);
		
		String dbAdminUserPK = serviceareaDAO.selectServiceAdminPK(vo.getSsa_area_key());
		if(!EgovStringUtil.equals(vo.getSsa_admin_id(), dbAdminUserPK))
			serviceareaDAO.updateServiceAreaMeta(vo);
		
		List<ServiceAreaManagerVO> serviceareaCarrerInfoList = serviceareaDAO.selectServiceAreaManagerInfoList(vo.getSsa_area_key());
		
		/**
		 * 휴게소 관리자  변동이 있다면 삭제후 재입력
		 */
		boolean managerModiChk = false;
		if(vo.getServiceAreaManagerVOList().size() != serviceareaCarrerInfoList.size()){
			managerModiChk = true;
		}else{
			for(int i=0;i<serviceareaCarrerInfoList.size();i++){
				ServiceAreaManagerVO db = serviceareaCarrerInfoList.get(i);
				ServiceAreaManagerVO paramVO = vo.getServiceAreaManagerVOList().get(i);
				if(!db.getSsm_job_title().equals(paramVO.getSsm_job_title())){
					managerModiChk = true;
					break;
				}
				if(!db.getSsm_manager_name().equals(paramVO.getSsm_manager_name())){
					managerModiChk = true;
					break;
				}
				if(!db.getSsm_manager_tel().equals(paramVO.getSsm_manager_tel())){
					managerModiChk = true;
					break;
				}
			}
		}
		if(managerModiChk){
			serviceareaDAO.deleteServiceAreaManager(vo.getSsa_area_key());
			this.insertServiceAreaManagerInfo(vo);
		}
	}
	
	/**
	 * 휴게소 정보 리스트 및 전체 카운트
	 */
	public Map selectServiceAreaList(ServiceAreaVO vo) throws Exception{
		Map resultMap = new HashMap();
		resultMap.put("resultList", serviceareaDAO.selectServiceAreaList(vo));
		resultMap.put("resultListCnt", serviceareaDAO.selectServiceAreaListCnt(vo));
		resultMap.put("zoneList", codeService.selectCodeInfoList(propertiesService.getString("scc.codekey.zone")));
		return resultMap;
	}
	/**
	 * 휴게소 정보 조회
	 */
	public ServiceAreaVO selectServiceAreaInfo(ServiceAreaVO vo) throws Exception{
		return serviceareaDAO.selectServiceAreaInfo(vo);
	}
	
	/**
	 * 휴게소 기타 정보
	 */
	public Map selectServiceAreaOtherInfo() throws Exception{
		Map resultMap = new HashMap();
		resultMap.put("zoneList", codeService.selectCodeInfoList(propertiesService.getString("scc.codekey.zone")));
		resultMap.put("userList", userService.selectUserInfoList());
		return resultMap;
	}
	
	/**
	 * 휴게소 기타 정보
	 */
	public List selectServiceAreaList() throws Exception{
		return serviceareaDAO.selectServiceAreaList();
	}
	
	/**
	 * 관리자 로그인 정보 입력
	 */
	public void insertServiceAreaAdminInfo(ServiceAreaVO vo) throws Exception{
		serviceareaDAO.insertServiceAreaAdminInfo(vo);
	}
	/**
	 * 관리자 정보
	 */
	public ServiceAreaVO selectServiceAreaAdminInfo(ServiceAreaVO vo) throws Exception{
		return serviceareaDAO.selectServiceAreaAdminInfo(vo);
	}
	/**
	 * 관리자 정보 수정
	 */
	public void updateServiceAreaAdminInfo(ServiceAreaVO vo) throws Exception{
		serviceareaDAO.updateServiceAreaInfo(vo);
	}
	/**
	 * 관리자 로그인 정보 리스트 및 전체 카운트
	 */
	public Map selectServiceAreaAdminList(ServiceAreaVO vo) throws Exception{
		Map resultMap = new HashMap();
		resultMap.put("resultList", serviceareaDAO.selectServiceAreaAdminList(vo));
		resultMap.put("resultListCnt", serviceareaDAO.selectServiceAreaAdminListCnt(vo));
		return resultMap;
	}
	
	public String selectServiceAreaMaxAreaKey() throws Exception{
		return serviceareaDAO.selectServiceAreaMaxAreaKey();
	}
}
